package cn.summit.passwordsty;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author summit
 * @since 2021/8/14 19:26
 */
public class MessageDigestTest {

	public static void main(String[] args) throws NoSuchAlgorithmException {
		String md5 = getDigest("md5");
		System.out.println("md5:" + md5);

		System.out.println("sha1:" + getDigest("sha-1"));
		System.out.println("sha256:" + getDigest("sha-256"));
		System.out.println("sha512:" + getDigest("sha-512"));
	}

	private static String getDigest(String algorithm) throws NoSuchAlgorithmException {
		String input = "aa";
		MessageDigest instance = MessageDigest.getInstance(algorithm);
		byte[] digest = instance.digest(input.getBytes());
		return getHex(digest);
	}

	private static String getHex(byte[] digest) {
		StringBuilder sb = new StringBuilder();
		for (byte bt : digest) {
			String s = Integer.toHexString(bt & 0xff);
			if (s.length() < 2) {
				sb.append("0");
			}
			sb.append(s);
		}

		return sb.toString();
	}
}
